package com.mdd.store.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.common.entity.Region;
import com.mdd.common.mapper.RegionMapper;
import com.mdd.store.entity.Store;
import com.mdd.store.entity.StoreBinding;
import com.mdd.store.mapper.StoreBindingMapper;
import com.mdd.store.service.IStoreBindingService;
import com.mdd.store.vo.StoreVo;
import com.mdd.system.vo.RegionDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 门店（店铺）实现类
 * @author LikeAdmin
 */
@Service
@Transactional
public class StoreBindingServiceImpl extends ServiceImpl<StoreBindingMapper, StoreBinding> implements IStoreBindingService {
    @Resource
    private StoreBindingMapper storeBindingMapper;
    @Resource
    private RegionMapper regionMapper;

    @Override
    public List<StoreVo> getBindStoreBySpuId(Long spuId) {

        List<Store> storeList = storeBindingMapper.getBindStoreBySpuId(spuId);
        List<StoreVo> storeVoList = new ArrayList<>();
        storeList.stream().forEach( store -> {
            StoreVo storeVo = new StoreVo();
            BeanUtils.copyProperties(store,storeVo);

            Region province =regionMapper.selectById(store.getProvince());
            Region city =regionMapper.selectById(store.getCity());
            Region country =regionMapper.selectById(store.getCounty());
            RegionDetailVo provinceVo  =new RegionDetailVo();
            RegionDetailVo cityVo  =new RegionDetailVo();
            RegionDetailVo countryVo  =new RegionDetailVo();

            BeanUtils.copyProperties(province, provinceVo);
            BeanUtils.copyProperties(city, cityVo);
            BeanUtils.copyProperties(country, countryVo);
            storeVo.setProvince(provinceVo);
            storeVo.setCity(cityVo);
            storeVo.setCounty(countryVo);

            storeVoList.add(storeVo);
        });
        return storeVoList;
    }
}
